﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hired.Classes
{
    public class DBJobTitleHits
    {
        //create job title hits
        public static bool CreateJobTitleHits(JobTitleHit jth, JobseekerAccount jsa)
        {
            jth.JobTitleHitsID = "tempvalue";
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(jsa);
                    jsa.JobTitleHits.Add(jth);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //retrieve job title hits
        public static JobTitleHit RetrieveJobTitleHits(string jobTitleHitsID)
        {
            JobTitleHit jobTitleHits = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                jobTitleHits =
                (from jth in db.JobTitleHits
                 where jth.JobTitleHitsID == jobTitleHitsID
                 select jth).SingleOrDefault();
            }
            return jobTitleHits;
        }

        //retrieve job title hits by JobseekerAccountID and Title
        public static JobTitleHit RetrieveJobTitleHitsByJAccountIDAndTitle(string jAccountID, string title)
        {
            JobTitleHit jobTitleHits = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                jobTitleHits =
                (from jth in db.JobTitleHits
                 where jth.JAccountID == jAccountID && jth.Title == title
                 select jth).SingleOrDefault();
            }
            return jobTitleHits;
        }

        //update job title hits
        public static bool UpdateJobTitleHits(JobTitleHit jth)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    JobTitleHit jobTitleHitsToUpdate = RetrieveJobTitleHits(jth.JobTitleHitsID);
                    db.Attach(jobTitleHitsToUpdate);
                    jobTitleHitsToUpdate.JAccountID = jth.JAccountID;
                    jobTitleHitsToUpdate.Title = jth.Title;
                    jobTitleHitsToUpdate.Count = jth.Count;
 
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        //delete job title hits
        public static bool DeleteJobTitleHits(JobTitleHit jth)
        {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    JobTitleHit deleteJobTitleHits = RetrieveJobTitleHits(jth.JobTitleHitsID);
                    db.Attach(deleteJobTitleHits);
                    db.DeleteObject(deleteJobTitleHits);
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //check if for this Jobseeker Account, is there a row of JobTitleHits for this certain title already
        public static bool CheckJAccountJobTitleHitsForTitleExist(string jAccountID, string title)
        {
            try
            {
                JobTitleHit jobTitleHitsObject = null;
                using (hireddbEntities db = new hireddbEntities())
                {
                    jobTitleHitsObject =
                    (from jth in db.JobTitleHits
                     where jth.JAccountID == jAccountID && jth.Title == title
                     select jth).SingleOrDefault();
                }

                if (jobTitleHitsObject == null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        //increase the count of job title hits for a Jobseeker Account for a certain title
        public static bool IncrementJobTitleHit(string JAccountID, string title)
        {
            try
            {
                bool incremented = false;
                JobTitleHit jobTitleHitsToIncreaseCount = RetrieveJobTitleHitsByJAccountIDAndTitle(JAccountID, title);
                jobTitleHitsToIncreaseCount.Count++;
                incremented = UpdateJobTitleHits(jobTitleHitsToIncreaseCount);
                return incremented;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}